// pages/userManage/userManage.js
import Toast from '@vant/weapp/toast/toast';
import Dialog from '@vant/weapp/dialog/dialog';
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    showEmpty: false,
    optionOpenid: '',
    show: false,
    type: 1,
    actions: [{
        name: '设为管理员',
      },
      {
        name: '管理 Ta 的药品',
      },
      {
        name: '删除该用户',
        color: '#FF3300'
      }
    ],
    description: "",
    userList: []
  },

  async getUserList() {
    // 请求用户列表
    const res = await app.call({
      path: '/user/getUserList',
      method: 'GET'
    })
    console.log(res)
    if (res.code === 0) {
      console.log(res.data)
      this.setData({
        userList: res.data.userList,
      })
    } else {
      Toast.fail(res.errorMsg);
    }

    if (this.data.userList.length === 0) {
      this.setData({
        showEmpty: true
      })
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  async onLoad(options) {
    // 请求用户类型
    let res = await app.call({
      path: '/user/getUserType',
      method: 'GET'
    })

    console.log(res)
    if (res.code === 0) {
      console.log(res.data)
      this.setData({
        type: res.data.type,
      })
      console.log(this.data.type)
      if (this.data.type != 2) {
        this.setData({
          actions: this.data.actions.slice(1)
        })
      }
    } else {
      Toast.fail(res.errorMsg);
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.getUserList();
  },
  onOption(event) {
    const user = event.currentTarget.dataset.user;
    console.log(user);
    const actions0 = this.data.actions;
    if (this.data.type == 2) {
      actions0[0].name = user.type === 0 ? "设为管理员" : "设为用户";
    } else {
      actions0[1].disabled = user.type === 0 ? false : true;
      actions0[1].color = user.type === 0 ? '#FF3300' : null
    }

    this.setData({
      show: true,
      optionOpenid: user.openid,
      actions: actions0,
      description: user.openid
    });
    console.log(this.data.actions)
  },
  onClose() {
    this.setData({
      show: false
    });
  },
  async onSelect(event) {
    const openid = this.data.optionOpenid
    const action = event.detail.name
    let res
    switch (action) {
      case "设为管理员":
        console.log("设为管理员")
        res = await app.call({
          path: 'user/updateUserType',
          method: 'PUT',
          data:{
            openid: openid,
            "type":1
          }
        })
        console.log(res)
        if (res.code === 0) {
          Toast.success("成功");
          this.getUserList();
        } else {
          Toast.fail(res.errorMsg);
        }
        break
      case "设为用户":
        console.log("设为用户")
        res = await app.call({
          path: 'user/updateUserType',
          method: 'PUT',
          data:{
            openid: openid,
            "type":0
          }
        })
        console.log(res)
        if (res.code === 0) {
          Toast.success("成功");
          this.getUserList();
        } else {
          Toast.fail(res.errorMsg);
        }
        break
      case "管理 Ta 的药品":
        console.log("管理 Ta 的药品")
        wx.navigateTo({
          url: '/pages/resDrugList/resDrugList?from=user&openid=' + openid,
        })
        break
      case "删除该用户":
        console.log("删除该用户")
        Dialog.confirm({
          message: '确定删除 ' + openid + ' 吗？',
        }).then(async () => {
          const res = await app.call({
            path: '/user/deleteUser?openid='+openid,
            method: 'DELETE',
          })
          console.log(res)
          if (res.code === 0) {
            Toast.success("删除成功");
            this.getUserList();
          } else {
            Toast.fail(res.errorMsg);
          }
        }).catch(() => {
          // on cancel
        });
        break
    }
  },
})